package br.ufpe.abaco.ParserGen.lexer;

class ArrayOps {

    /**
     *  Description of the Method
     *
     *@param  src  Description of the Parameter
     *@param  t    Description of the Parameter
     *@return      Description of the Return Value
     */
    static boolean contains(RegExp src[], RegExp t) {
        for (int c = 0; c < src.length; c++) {
            if (src[c] == t) {
                return true;
            }
        }
        return false;
    }


    /**
     *  Description of the Method
     *
     *@param  r1  Description of the Parameter
     *@param  r2  Description of the Parameter
     *@return     Description of the Return Value
     */
    static RegExp[] concat(RegExp r1[], RegExp r2[]) {
        if (r1.length == 0) {
            return r2;
        } else if (r2.length == 0) {
            return r1;
        }
        RegExp resul[] = new RegExp[r1.length + r2.length];
        int posR1;
        int posR2;
        int posResul;
        posR1 = posR2 = posResul = 0;
        while (posR1 < r1.length && posR2 < r2.length) {
            int id1 = r1[posR1].id;
            int id2 = r2[posR2].id;
            if (id1 == id2) {
                resul[posResul++] = r1[posR1++];
                posR2++;
            } else if (id1 < id2) {
                resul[posResul++] = r1[posR1++];
            } else {
                resul[posResul++] = r2[posR2++];
            }
        }
        if (posR1 < r1.length) {
            for (; posR1 < r1.length; posR1++) {
                resul[posResul++] = r1[posR1];
            }
        } else {
            for (; posR2 < r2.length; posR2++) {
                resul[posResul++] = r2[posR2];
            }
        }
        if (posResul < resul.length) {
            RegExp resul2[] = new RegExp[posResul];
            while (posResul > 0) {
                posResul--;
                resul2[posResul] = resul[posResul];
            }
            resul = resul2;
        }
        return resul;
    }


    /**
     *  Description of the Method
     *
     *@param  r1  Description of the Parameter
     *@param  r2  Description of the Parameter
     *@return     Description of the Return Value
     */
    static boolean equals(RegExp r1[], RegExp r2[]) {
        return compare(r1, r2) == 0;
    }


    /**
     *  Description of the Method
     *
     *@param  r1  Description of the Parameter
     *@param  r2  Description of the Parameter
     *@return     Description of the Return Value
     */
    static int compare(RegExp r1[], RegExp r2[]) {
        if (r1.length != r2.length) {
            return r1.length - r2.length;
        }
        for (int c = 0; c < r1.length; c++) {
            if (r1[c] != r2[c]) {
                return r1[c].id - r2[c].id;
            }
        }
        return 0;
    }


    /**
     *  Description of the Method
     *
     *@param  r  Description of the Parameter
     *@return    Description of the Return Value
     */
/*    public static String print(RegExp r[]) {
        String resul = " [ ";
        for (int x = 0; x < r.length; x++) {
            resul += r[x].label + " ";
        }
        return resul + "] ";
    }
*/
}
